<template>
  <el-dialog width="60%" top="10vh" :title="!dataForm.id ? '新增' : '修改'" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="100px">
      <el-form-item label="类型名称" prop="typeName">
        <el-input v-model="dataForm.typeName" placeholder="请输入类型名称"></el-input>
      </el-form-item>
      <el-form-item label="急救名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="请输入急救名称"></el-input>
      </el-form-item>
      <el-form-item label="急救内容" prop="content">
        <Tinymce  v-model="dataForm.content" v-if="visible" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" size="small" @click="dataFormSubmit()">确定</el-button>
      <el-button size="small" @click="visible = false">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getFirstAidInfo, createFirstAid, updateFirstAid } from '@/api/knowledge/firstaid';
import Tinymce from '@/components/Tinymce';

const defaultDataForm = {
  id: '',
  typeId: '',
  typeName: '',
  name: '',
  content: '',
}

export default {
  components: {
    Tinymce,
  },
  data() {
    return {
      dataForm: Object.assign({}, defaultDataForm),
      visible: false,
      dataRule: {
        typeId: [
          { required: true, message: '类型ID不能为空', trigger: 'blur' }
        ],
        typeName: [
          { required: true, message: '类型名称不能为空', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '急救名称不能为空', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '急救内容不能为空', trigger: 'blur' }
        ],
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          getFirstAidInfo(this.dataForm.id).then(res => {
            if (res && res.code === 200) {
              this.dataForm.typeId = res.data.typeId;
              this.dataForm.typeName = res.data.typeName;
              this.dataForm.name = res.data.name;
              this.dataForm.content = res.data.content;
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          let params = {
            id: this.dataForm.id,
            typeId: this.dataForm.typeId,
            typeName: this.dataForm.typeName,
            name: this.dataForm.name,
            content: this.dataForm.content,
          }
          if (!this.dataForm.id) {
            // 创建
            createFirstAid(params).then(response => {
              this.$refs['dataForm'].resetFields();
              this.dataForm = Object.assign({}, defaultDataForm);
              this.$message({
                message: '提交成功',
                type: 'success',
                duration: 1000
              })
              this.visible = false
              this.$emit('refreshDataList')
            })
          } else {
            // 更新
            updateFirstAid(params).then(response => {
              this.$refs['dataForm'].resetFields();
              this.$message({
                message: '修改成功',
                type: 'success',
                duration: 1000
              })
              this.visible = false
              this.$emit('refreshDataList')
            })
          }
        }
      })
    },
  }
}
</script>
